package args

import "errors"

type Args struct {
	values    []any
	readIndex int
}

func NewEmpty() *Args {
	return &Args{}
}

func (a *Args) Len() int {
	if a.values == nil {
		return 0
	}
	return len(a.values)
}

func (a *Args) IsEmptyArgs() bool {
	return a.values == nil
}

func (a *Args) Get(index int) any {
	if index < 0 || index >= len(a.values) {
		return nil
	}
	return a.values[index]
}

func (a *Args) ReadNext() any {
	if a.readIndex < 0 || a.readIndex >= len(a.values) {
		return nil
	}
	rs := a.values[a.readIndex]
	a.readIndex++
	return rs
}

func (a *Args) Reset() {
	a.readIndex++
}

func ValueOf(args ...any) (*Args, error) {
	length := len(args)
	if length == 0 {
		return nil, errors.New("args is empty")
	}
	argsRs := &Args{values: make([]any, 0, length), readIndex: 0}
	argsRs.values = append(argsRs.values, args...)
	return argsRs, nil
}
